package com.adou.p2p.page;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class ResponseVo<T>  {
	private int code = ErrorCode.SUCCESS.getCode();

	private String msg = ErrorCode.SUCCESS.getMsg();

	private T data;

	private Page page;

	public ResponseVo(T data, Page page) {
		this.data = data;
		this.page = page;
	}

	public ResponseVo(ErrorCode  code, T data, Page page) {
		this.code = code.getCode();
		this.msg = code.getMsg();
		this.data = data;
		this.page = page;
	}

	public ResponseVo(int code, String msg, T data, Page page) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.page = page;
	}

	public ResponseVo(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public ResponseVo(ErrorCode  code, T data) {
		this.code = code.getCode();
		this.msg = code.getMsg();
		this.data = data;
	}

	public ResponseVo(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ResponseVo(T data) {
		this.data = data;
	}

	// 构造成功
	public static <T> ResponseVo success(){
		return new ResponseVo (ErrorCode.SUCCESS);
	}

	public static <T> ResponseVo success(T data){
		return new ResponseVo (ErrorCode.SUCCESS, data);
	}

	public static <T> ResponseVo success(int code, String msg, T data){
		return new ResponseVo (code, msg, data);
	}

	public static <T> ResponseVo success(int code, String msg, T data, Page page){
		return new ResponseVo (code, msg, data, page);
	}

	public static <T> ResponseVo success(T data, Page page){
		return new ResponseVo (  ErrorCode.SUCCESS, data, page);
	}

	public static <T> ResponseVo<List<T>> success(PageResult<T> pageResult){
		return new ResponseVo (pageResult.getContent(), 
				new Page(pageResult.getPageNumber()+1, pageResult.getPageSize(), (int)pageResult.getTotalElements()));
	}


	//失败构造方法
	public static <T> ResponseVo failtrue(ErrorCode code){
	return new ResponseVo(code.getCode(), code.getMsg());
	}

	public static <T> ResponseVo failtrue(T data){
	return new ResponseVo(data);
	}

	public static <T> ResponseVo failtrue(ErrorCode code, T data){
	return new ResponseVo(code.getCode(), code.getMsg(), data);
	}

	public static  ResponseVo failtrue(Integer code, String msg){
	return new ResponseVo(code, msg);
	}

	public static  ResponseVo failtrue(ErrorCode code, String msg){
	return new ResponseVo(code.getCode(), msg);
	}

	public int getCode(){
		return code;
	}

	public void setCode(int code){
		this.code = code;
	}

	public String getMsg(){
	return msg;
	}

	public void  setMsg(String msg){
		this.msg= msg;
	}
	public T getData(){
		return data;
	}

	public void setData(T data){
		this.data= data;
	}

	public Page getPage(){
		return page;
	}

	public void sePage(Page page){
		this.page= page;
	}

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	public static class Page{
		// 当前页
		private int pageNumber = 0;
	
		//总记录条数
		private int totalElements;
	
		//默认页大小
		private int pageSize = PageConsts.DEFAULT_PAGE_SIZE;
	
		//总页数
		private int totalPages;
	
		public Page(int pageNumber, int pageSize, int totalElements) {
			if(pageSize <0) {
				pageSize = PageConsts.DEFAULT_PAGE_SIZE;
			}
		
			if(totalElements < 0) {
				totalElements = 0;
			}
		
			this.pageNumber = pageNumber;
			this.totalElements   = totalElements ;
			this.pageSize = pageSize;
		
			if(totalElements % pageSize == 0) {
				this.totalPages = totalElements / pageSize;
			} else {
				this.totalPages = totalElements / pageSize + 1;
			}
		}
	}
}
